package com.example.smartagriculture.runner;

import blockchain.json.BlockChainJson;
import blockchain.net.NetServerScanner;
import cn.hutool.cron.CronUtil;
import com.example.smartagriculture.entity.SocketEntity;
import com.example.smartagriculture.handler.NettyServerHandler;
import com.example.smartagriculture.listener.NettyListener;
import lombok.extern.slf4j.Slf4j;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.InetSocketAddress;

@Component
@Slf4j
public class LaunchRunner
        implements CommandLineRunner
{
    @Resource
    private NettyListener nettyListener;

    @Resource
    private SocketEntity socketProperties;


    @Override
    public void run(String... args) throws Exception {
        {
            log.info(NetServerScanner.serverRun());
            log.info(NetServerScanner.defaultBlock());
/*            BlockChainJson blockChainJson3 = new BlockChainJson();
            blockChainJson3.personInCharge = "元神启动！";
            blockChainJson3.productID = "圆神启动！";
            blockChainJson3.productionLocation = "圆神启动基地";
            blockChainJson3.humidity = "100";
            blockChainJson3.temperature = "101";
            NetServerScanner.add(blockChainJson3);*/
            log.info("添加成功");
        }

        TaskRunner();
        InetSocketAddress address = new InetSocketAddress(socketProperties.getHost(),socketProperties.getPort());
        log.info("netty服务器启动地址:"+socketProperties.getHost());
        nettyListener.start(address);
    }
    /**
     * 执行正在运行的任务
     */
    private  void TaskRunner() {
        /**
         * 任务队列启动
         */
        CronUtil.setMatchSecond(true);
        CronUtil.start();
        log.info("\n-----------------------任务服务启动------------------------\n\t" +
                        "当前正在启动的{}个任务"+
                        "\n-----------------------------------------------------------\n\t"
                , CronUtil.getScheduler().size()

        );
    }
}
